package com.gxm.base.datatype.date;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * @创建人 郭训民
 * @创建时间 2019/11/9
 * @描述
 */
public class Jdk8 {
    /**
     * @描述 java.time.LocalDate 功能演示
     * @参数
     * @返回值
     * @创建人 郭训民
     * @创建时间 2019/11/9
     * @修改人和其它信息
     */
    @Test
    public void Test1() {
        //Current Date
        LocalDate today = LocalDate.now();
        System.out.println("Current Date=" + today);
//Creating LocalDate by providing input arguments
        LocalDate firstDay_2014 = LocalDate.of(2014, Month.JANUARY, 1);
        System.out.println("Specific Date=" + firstDay_2014);
//Try creating date by providing invalid inputs
//LocalDate feb29_2014 = LocalDate.of(2014, Month.FEBRUARY, 29);
//Exception in thread "main" java.time.DateTimeException:
//Invalid date 'February 29' as '2014' is not a leap year
//Current date in "Asia/Kolkata", you can get it from ZoneId javadoc
        LocalDate todayKolkata = LocalDate.now(ZoneId.of("Asia/Kolkata"));
        System.out.println("Current Date in IST=" + todayKolkata);
//java.time.zone.ZoneRulesException: Unknown time-zone ID: IST
//LocalDate todayIST = LocalDate.now(ZoneId.of("IST"));
//Getting date from the base date i.e 01/01/1970
        LocalDate dateFromBase = LocalDate.ofEpochDay(365);
        System.out.println("365th day from base date= " + dateFromBase);
        LocalDate hundredDay2014 = LocalDate.ofYearDay(2014, 100);
        System.out.println("100th day of 2014=" + hundredDay2014);
    }

    /**
     * @描述 java.time.Instant 功能演示 Instant 类是用在机器可读的时间格式上的，它以 Unix 时间戳的形式存储日期时间，
     * @参数
     * @返回值
     * @创建人 郭训民
     * @创建时间 2019/11/9
     * @修改人和其它信息
     */
    @Test
    public void Test2() {
//Current timestamp
        Instant timestamp = Instant.now();
        System.out.println("Current Timestamp = "+timestamp);
//Instant from timestamp
        Instant specificTime = Instant.ofEpochMilli(timestamp.toEpochMilli());
        System.out.println("Specific Time = "+specificTime);
//Duration example
        Duration thirtyDay = Duration.ofDays(30);
        System.out.println(thirtyDay);
    }

    @Test
    public void DateParseFormatExample(){
        //Format examples
        LocalDate date = LocalDate.now();
//default format
        System.out.println("Default format of LocalDate="+date);
//specific format
        System.out.println(date.format(DateTimeFormatter.ofPattern("d::MMM::uuuu")));
        System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));
        LocalDateTime dateTime = LocalDateTime.now();
//default format
        System.out.println("Default format of LocalDateTime="+dateTime);
//specific format
        System.out.println(dateTime.format(DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss")));
        System.out.println(dateTime.format(DateTimeFormatter.BASIC_ISO_DATE));
        Instant timestamp = Instant.now();
//default format
        System.out.println("Default format of Instant="+timestamp);
//Parse examples
        LocalDateTime dt = LocalDateTime.parse("27::Apr::2014 21::39::48",
                DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss"));
        System.out.println("Default format after parsing = "+dt);
    }
}
